package com.jed.xxh.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.jed.xxh.R;
import com.jed.xxh.entity.MyUser;
import com.jed.xxh.utils.L;
import com.jed.xxh.utils.T;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 项目名：SmartButler<br/>
 * 包名：  com.jed.xxh.ui<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/25 16:08<br/>
 * 描述：注册用户<br/>
 */
public class RegisteredActivity extends BaseActivity implements View.OnClickListener {

    Boolean sex = true;//性别
    private EditText reg_et_user;//用户名
    private EditText reg_et_age;//年龄
    private EditText reg_et_desc;//简介
    private EditText reg_et_pass;//密码
    private EditText reg_et_password;//密码
    private EditText reg_et_email;//邮箱
    private Button reg_btnRegistered;//注册按钮
    private RadioGroup reg_mRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);

        intiView();
    }

    /**
     * 初始化
     */
    private void intiView() {
        reg_et_user = findViewById(R.id.reg_et_user);
        reg_et_age = findViewById(R.id.reg_et_age);
        reg_et_desc = findViewById(R.id.reg_et_desc);
        reg_et_pass = findViewById(R.id.reg_et_pass);
        reg_et_password = findViewById(R.id.reg_et_password);
        reg_et_email = findViewById(R.id.reg_et_email);
        reg_btnRegistered = findViewById(R.id.reg_btnRegistered);
        reg_mRadioGroup = findViewById(R.id.reg_mRadioGroup);

        reg_btnRegistered.setOnClickListener(RegisteredActivity.this);

    }

    /**
     * 注册页面点击事件
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.reg_btnRegistered:
                //获取输入框的值
                String name = reg_et_user.getText().toString().trim();
                String age = reg_et_age.getText().toString().trim();
                String desc = reg_et_desc.getText().toString().trim();
                String pass = reg_et_pass.getText().toString().trim();
                String password = reg_et_password.getText().toString().trim();
                String email = reg_et_email.getText().toString().trim();

                boolean isEmpty = !TextUtils.isEmpty(name)
                        && !TextUtils.isEmpty(age)
                        && !TextUtils.isEmpty(pass)
                        && !TextUtils.isEmpty(password)
                        && !TextUtils.isEmpty(email);
                if (isEmpty) {
                    //判断两次输入的密码是否一致
                    if (pass.equals(password)) {
                        //判断性别的选中状态
                        reg_mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                                if (i == R.id.reg_rb_boy) {
                                    sex = true;
                                } else if (i == R.id.reg_rb_girl) {
                                    sex = false;
                                }
                            }
                        });
                        //判断是否填写的简介
                        if (TextUtils.isEmpty(desc)) {
                            desc = getResources().getString(R.string.text_nothing);
                        }
                        //开始注册
                        MyUser myUser = new MyUser();
                        myUser.setUsername(name);
                        myUser.setPassword(pass);
                        myUser.setAge(Integer.parseInt(age));
                        myUser.setDesc(desc);
                        myUser.setEmail(email);
                        myUser.setSex(sex);
                        myUser.signUp(new SaveListener<MyUser>() {
                            @Override
                            public void done(MyUser myUser, BmobException e) {
                                if (e == null) {
                                    T.showShortToast(RegisteredActivity.this.getString(R.string.text_registered_successful));
                                } else {
                                    L.i("注册失败=====");
                                    T.showShortToast(RegisteredActivity.this.getString(R.string.text_registered_failure));
                                }
                            }
                        });
                    } else {
                        T.showShortToast(this.getString(R.string.text_two_input_not_consistent));
                    }
                } else {
                    T.showShortToast(this.getString(R.string.text_tost_empty));
                }
                break;
        }
    }
}
